package club.lemos;

import antlr4.HelloLexer;
import antlr4.HelloParser;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;

import java.io.IOException;
import java.io.InputStream;

public class Hello {

    public static void main(String[] args) {
        try (InputStream inputIs = Hello.class.getResourceAsStream("/hello.txt");) {
            if (inputIs == null) {
                System.out.println("文件未找到");
                return;
            }
            HelloLexer lexer = new HelloLexer(CharStreams.fromStream(inputIs));
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            HelloParser parser = new HelloParser(tokens);
            ParseTree tree = parser.r();
//            tree.accept(new HelloBaseVisitor<>());
//            new HelloBaseVisitor<>().visit(tree);
            ParseTreeWalker walker = new ParseTreeWalker();
            walker.walk(new HelloWalker(), tree);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}